-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Collection prefix support for MongoDB job repository #5031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Add collectionPrefix parameter to EnableMongoJobRepository annotation - Update MongoDB DAO classes to support configurable collection names - Maintain backward compatibility with default "BATCH_" prefix Signed-off-by: Myeongha Shin <[email protected]>
gyuyeol
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! I've added some comments
| mongoTemplate.createCollection("BATCH_SEQUENCES"); | ||
| mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
| .insertOne(new Document(Map.of("_id", PREFIX + "JOB_INSTANCE_SEQ", "count", 0L))); | ||
| mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
| .insertOne(new Document(Map.of("_id", PREFIX + "JOB_EXECUTION_SEQ", "count", 0L))); | ||
| mongoTemplate.getCollection("BATCH_SEQUENCES") | ||
| .insertOne(new Document(Map.of("_id", PREFIX + "STEP_EXECUTION_SEQ", "count", 0L))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BATCH_SEQUENCES collection should also have a prefix. Otherwise, batch jobs with different prefixes will end up sharing the same collection. I understand that it works correctly due to the prefix on the document ID. However, using the same collection will likely cause confusion.
Also, class MongoSequenceIncrementer needs to be modified to apply the prefix. This class also has the BATCH_SEQUENCES collection hardcoded.
| this.jobExecutionIncrementer = new MongoSequenceIncrementer(mongoOperations, | ||
| collectionPrefix + JOB_EXECUTIONS_SEQUENCE_NAME); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if we could pass the collectionPrefix as a parameter and use it internally as a prefix for both the collection name and the sequence ID.
📝 Description
This PR adds collection prefix support for MongoDB job repository, mirroring the existing tablePrefix functionality in JDBC job repository. This enables multi-application support, environment isolation, and easier management of MongoDB collections in Spring Batch deployments.
🎯 Motivation
Currently, MongoDB job repository uses hardcoded collection names (BATCH_JOB_INSTANCE, BATCH_JOB_EXECUTION, etc.), making it difficult to:
Run multiple Spring Batch applications against the same MongoDB database
Isolate different environments (dev, test, prod)
Manage collections with custom naming conventions
🔧 Changes
EnableMongoJobRepository: Add collectionPrefix parameter (defaults to "BATCH_")
MongoJobRepositoryFactoryBean: Add collection prefix support
MongoDB DAO classes: Support configurable collection names via constructor parameters
BatchRegistrar: Pass collection prefix from annotation to factory bean
Tests: Add integration tests for collection prefix functionality
💡 Usage Example
This will create collections like:
MY_APP_JOB_INSTANCE
MY_APP_JOB_EXECUTION
MY_APP_STEP_EXECUTION
Closes: collectionPrefix for MongoDB Job repository #4980